<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="订单编号" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="订单号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="行号" prop="lineNo">
        <el-input
          v-model="queryParams.lineNo"
          placeholder="请输入行号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="SPU编号" prop="spuId">
        <el-input
          v-model="queryParams.spuId"
          placeholder="请输入SPU编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="SKU编号" prop="skuId">
        <el-input
          v-model="queryParams.skuId"
          placeholder="请输入SKU编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="SKU编码" prop="skuCode">
        <el-input
          v-model="queryParams.skuCode"
          placeholder="请输入SKU编码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="英文商品名称" prop="productNameEn">
        <el-input
          v-model="queryParams.productNameEn"
          placeholder="请输入英文商品名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="品牌名称" prop="brandName">
        <el-input
          v-model="queryParams.brandName"
          placeholder="请输入品牌名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="分类名称" prop="categoryName">
        <el-input
          v-model="queryParams.categoryName"
          placeholder="请输入分类名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="规格值JSON" prop="specValues">
        <el-input
          v-model="queryParams.specValues"
          placeholder="请输入规格值JSON"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="条形码" prop="barcode">
        <el-input
          v-model="queryParams.barcode"
          placeholder="请输入条形码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="UPC码" prop="upcCode">
        <el-input
          v-model="queryParams.upcCode"
          placeholder="请输入UPC码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="HS编码" prop="hsCode">
        <el-input
          v-model="queryParams.hsCode"
          placeholder="请输入HS编码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="原产国代码" prop="originCountry">
        <el-input
          v-model="queryParams.originCountry"
          placeholder="请输入原产国代码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="购买数量" prop="quantity">
        <el-input
          v-model="queryParams.quantity"
          placeholder="请输入购买数量"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="单价" prop="unitPrice">
        <el-input
          v-model="queryParams.unitPrice"
          placeholder="请输入单价"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="总价" prop="totalPrice">
        <el-input
          v-model="queryParams.totalPrice"
          placeholder="请输入总价"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="货币代码" prop="currencyCode">
        <el-input
          v-model="queryParams.currencyCode"
          placeholder="请输入货币代码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="折扣金额" prop="discountAmount">
        <el-input
          v-model="queryParams.discountAmount"
          placeholder="请输入折扣金额"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="优惠券金额" prop="couponAmount">
        <el-input
          v-model="queryParams.couponAmount"
          placeholder="请输入优惠券金额"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="最终价格" prop="finalPrice">
        <el-input
          v-model="queryParams.finalPrice"
          placeholder="请输入最终价格"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="重量（kg）" prop="weight">
        <el-input
          v-model="queryParams.weight"
          placeholder="请输入重量（kg）"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="长度（cm）" prop="length">
        <el-input
          v-model="queryParams.length"
          placeholder="请输入长度（cm）"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="宽度（cm）" prop="width">
        <el-input
          v-model="queryParams.width"
          placeholder="请输入宽度（cm）"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="高度（cm）" prop="height">
        <el-input
          v-model="queryParams.height"
          placeholder="请输入高度（cm）"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="体积（立方米）" prop="volume">
        <el-input
          v-model="queryParams.volume"
          placeholder="请输入体积（立方米）"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="成本价" prop="costPrice">
        <el-input
          v-model="queryParams.costPrice"
          placeholder="请输入成本价"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="利润金额" prop="profitAmount">
        <el-input
          v-model="queryParams.profitAmount"
          placeholder="请输入利润金额"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="利润率" prop="profitRate">
        <el-input
          v-model="queryParams.profitRate"
          placeholder="请输入利润率"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="税率" prop="taxRate">
        <el-input
          v-model="queryParams.taxRate"
          placeholder="请输入税率"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="税费" prop="taxAmount">
        <el-input
          v-model="queryParams.taxAmount"
          placeholder="请输入税费"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="保修期（天）" prop="warrantyPeriod">
        <el-input
          v-model="queryParams.warrantyPeriod"
          placeholder="请输入保修期（天）"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="是否礼品" prop="isGift">
        <el-select
          v-model="queryParams.isGift"
          placeholder="请选择是否礼品"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="礼品留言" prop="giftMessage">
        <el-input
          v-model="queryParams.giftMessage"
          placeholder="请输入礼品留言"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="是否定制商品" prop="isCustomized">
        <el-select
          v-model="queryParams.isCustomized"
          placeholder="请选择是否定制商品"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="定制信息JSON" prop="customizationInfo">
        <el-input
          v-model="queryParams.customizationInfo"
          placeholder="请输入定制信息JSON"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="发货截止时间" prop="deliveryDeadline">
        <el-date-picker
          v-model="queryParams.deliveryDeadline"
          value-format="YYYY-MM-DD"
          type="date"
          placeholder="选择发货截止时间"
          clearable
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="预计送达日期" prop="expectedDeliveryDate">
        <el-date-picker
          v-model="queryParams.expectedDeliveryDate"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="履约状态（10待处理 20已分配 30拣货中 40已拣货 50已打包 60已发货 70已送达）" prop="fulfillmentStatus">
        <el-select
          v-model="queryParams.fulfillmentStatus"
          placeholder="请选择履约状态（10待处理 20已分配 30拣货中 40已拣货 50已打包 60已发货 70已送达）"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="分配仓库编号" prop="allocatedWarehouseId">
        <el-input
          v-model="queryParams.allocatedWarehouseId"
          placeholder="请输入分配仓库编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="已分配数量" prop="allocatedQuantity">
        <el-input
          v-model="queryParams.allocatedQuantity"
          placeholder="请输入已分配数量"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="已拣货数量" prop="pickedQuantity">
        <el-input
          v-model="queryParams.pickedQuantity"
          placeholder="请输入已拣货数量"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="已发货数量" prop="shippedQuantity">
        <el-input
          v-model="queryParams.shippedQuantity"
          placeholder="请输入已发货数量"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="已送达数量" prop="deliveredQuantity">
        <el-input
          v-model="queryParams.deliveredQuantity"
          placeholder="请输入已送达数量"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="已退货数量" prop="returnedQuantity">
        <el-input
          v-model="queryParams.returnedQuantity"
          placeholder="请输入已退货数量"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="已退款数量" prop="refundedQuantity">
        <el-input
          v-model="queryParams.refundedQuantity"
          placeholder="请输入已退款数量"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="取消原因" prop="cancelReason">
        <el-input
          v-model="queryParams.cancelReason"
          placeholder="请输入取消原因"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="queryParams.remark"
          placeholder="请输入备注"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['crossborder:order-item:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['crossborder:order-item:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
        <el-button
            type="danger"
            plain
            :disabled="isEmpty(checkedIds)"
            @click="handleDeleteBatch"
            v-hasPermi="['crossborder:order-item:delete']"
        >
          <Icon icon="ep:delete" class="mr-5px" /> 批量删除
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
        row-key="id"
        v-loading="loading"
        :data="list"
        :stripe="true"
        :show-overflow-tooltip="true"
        @selection-change="handleRowCheckboxChange"
    >
    <el-table-column type="selection" width="55" />
      <el-table-column label="订单项编号" align="center" prop="id" />
      <el-table-column label="订单编号" align="center" prop="orderId" />
      <el-table-column label="订单号" align="center" prop="orderNo" />
      <el-table-column label="行号" align="center" prop="lineNo" />
      <el-table-column label="SPU编号" align="center" prop="spuId" />
      <el-table-column label="SKU编号" align="center" prop="skuId" />
      <el-table-column label="SKU编码" align="center" prop="skuCode" />
      <el-table-column label="商品名称" align="center" prop="productName" />
      <el-table-column label="英文商品名称" align="center" prop="productNameEn" />
      <el-table-column label="商品图片" align="center" prop="productImage" />
      <el-table-column label="品牌名称" align="center" prop="brandName" />
      <el-table-column label="分类名称" align="center" prop="categoryName" />
      <el-table-column label="规格值JSON" align="center" prop="specValues" />
      <el-table-column label="条形码" align="center" prop="barcode" />
      <el-table-column label="UPC码" align="center" prop="upcCode" />
      <el-table-column label="HS编码" align="center" prop="hsCode" />
      <el-table-column label="原产国代码" align="center" prop="originCountry" />
      <el-table-column label="购买数量" align="center" prop="quantity" />
      <el-table-column label="单价" align="center" prop="unitPrice" />
      <el-table-column label="总价" align="center" prop="totalPrice" />
      <el-table-column label="货币代码" align="center" prop="currencyCode" />
      <el-table-column label="折扣金额" align="center" prop="discountAmount" />
      <el-table-column label="优惠券金额" align="center" prop="couponAmount" />
      <el-table-column label="最终价格" align="center" prop="finalPrice" />
      <el-table-column label="重量（kg）" align="center" prop="weight" />
      <el-table-column label="长度（cm）" align="center" prop="length" />
      <el-table-column label="宽度（cm）" align="center" prop="width" />
      <el-table-column label="高度（cm）" align="center" prop="height" />
      <el-table-column label="体积（立方米）" align="center" prop="volume" />
      <el-table-column label="成本价" align="center" prop="costPrice" />
      <el-table-column label="利润金额" align="center" prop="profitAmount" />
      <el-table-column label="利润率" align="center" prop="profitRate" />
      <el-table-column label="税率" align="center" prop="taxRate" />
      <el-table-column label="税费" align="center" prop="taxAmount" />
      <el-table-column label="保修期（天）" align="center" prop="warrantyPeriod" />
      <el-table-column label="是否礼品" align="center" prop="isGift" />
      <el-table-column label="礼品留言" align="center" prop="giftMessage" />
      <el-table-column label="是否定制商品" align="center" prop="isCustomized" />
      <el-table-column label="定制信息JSON" align="center" prop="customizationInfo" />
      <el-table-column
        label="发货截止时间"
        align="center"
        prop="deliveryDeadline"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="预计送达日期" align="center" prop="expectedDeliveryDate" />
      <el-table-column label="履约状态（10待处理 20已分配 30拣货中 40已拣货 50已打包 60已发货 70已送达）" align="center" prop="fulfillmentStatus" />
      <el-table-column label="分配仓库编号" align="center" prop="allocatedWarehouseId" />
      <el-table-column label="已分配数量" align="center" prop="allocatedQuantity" />
      <el-table-column label="已拣货数量" align="center" prop="pickedQuantity" />
      <el-table-column label="已发货数量" align="center" prop="shippedQuantity" />
      <el-table-column label="已送达数量" align="center" prop="deliveredQuantity" />
      <el-table-column label="已退货数量" align="center" prop="returnedQuantity" />
      <el-table-column label="已退款数量" align="center" prop="refundedQuantity" />
      <el-table-column label="取消原因" align="center" prop="cancelReason" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['crossborder:order-item:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['crossborder:order-item:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <OrderItemForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { isEmpty } from '@/utils/is'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { OrderItemApi, OrderItem } from '@/api/crossborder/orderitem'
import OrderItemForm from './OrderItemForm.vue'

/** 跨境电商订单明细 列表 */
defineOptions({ name: 'OrderItem' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<OrderItem[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  orderId: undefined,
  orderNo: undefined,
  lineNo: undefined,
  spuId: undefined,
  skuId: undefined,
  skuCode: undefined,
  productName: undefined,
  productNameEn: undefined,
  productImage: undefined,
  brandName: undefined,
  categoryName: undefined,
  specValues: undefined,
  barcode: undefined,
  upcCode: undefined,
  hsCode: undefined,
  originCountry: undefined,
  quantity: undefined,
  unitPrice: undefined,
  totalPrice: undefined,
  currencyCode: undefined,
  discountAmount: undefined,
  couponAmount: undefined,
  finalPrice: undefined,
  weight: undefined,
  length: undefined,
  width: undefined,
  height: undefined,
  volume: undefined,
  costPrice: undefined,
  profitAmount: undefined,
  profitRate: undefined,
  taxRate: undefined,
  taxAmount: undefined,
  warrantyPeriod: undefined,
  isGift: undefined,
  giftMessage: undefined,
  isCustomized: undefined,
  customizationInfo: undefined,
  deliveryDeadline: undefined,
  deliveryDeadline: [],
  expectedDeliveryDate: [],
  fulfillmentStatus: undefined,
  allocatedWarehouseId: undefined,
  allocatedQuantity: undefined,
  pickedQuantity: undefined,
  shippedQuantity: undefined,
  deliveredQuantity: undefined,
  returnedQuantity: undefined,
  refundedQuantity: undefined,
  cancelReason: undefined,
  remark: undefined,
  createTime: [],
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await OrderItemApi.getOrderItemPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await OrderItemApi.deleteOrderItem(id)
    message.success(t('common.delSuccess'))
    currentRow.value = {}
    // 刷新列表
    await getList()
  } catch {}
}

/** 批量删除跨境电商订单明细 */
const handleDeleteBatch = async () => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    await OrderItemApi.deleteOrderItemList(checkedIds.value);
    message.success(t('common.delSuccess'))
    await getList();
  } catch {}
}

const checkedIds = ref<number[]>([])
const handleRowCheckboxChange = (records: OrderItem[]) => {
  checkedIds.value = records.map((item) => item.id);
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await OrderItemApi.exportOrderItem(queryParams)
    download.excel(data, '跨境电商订单明细.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>